package com.sport.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sport.config.security.contents.MessageConstant;
import com.sport.entity.Sport;
import com.sport.entity.SportType;
import com.sport.mapper.SportTypeMapper;
import com.sport.service.SportTypeService;
import com.sport.utils.PageResult;
import com.sport.utils.QueryInfo;
import com.sport.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 阿杰
 * @create 2021-04-01 11:15
 */
@Service
public class SportTypeServiceImpl implements SportTypeService {

    @Autowired
    private SportTypeMapper sportTypeMapper;

    @Override
    public Result findPage(QueryInfo queryInfo) {
        PageHelper.startPage(queryInfo.getPageNumber(), queryInfo.getPageSize());
        Page<SportType> page = sportTypeMapper.findPage(queryInfo.getQueryString());
        if (page == null) {
            return new Result(false, MessageConstant.SPORT_SELECT_FAIL);
        }
        long total = page.getTotal();
        List<SportType> result = page.getResult();
        return new Result(true, MessageConstant.SPORT_SELECT_SUCCESS, new PageResult(total, result));
    }

    @Override
    public Result add(SportType sport) {
        SportType sportByName = sportTypeMapper.findByName(sport.getName());
        if (sportByName != null) {
            return new Result(false, MessageConstant.SPORT_EXIST);
        }
        int i = sportTypeMapper.insert(sport);
        if (i <= 0) {
            return new Result(false, MessageConstant.ADD_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SPORT_SUCCESS);
    }

    @Override
    public Result delete(Integer id) {
        if (id == null) {
            return new Result(false, MessageConstant.CHOOSE_SPORT);
        }
        int i = sportTypeMapper.delete(id);
        if (i <= 0) {
            return new Result(false, MessageConstant.DELETE_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SPORT_SUCCESS);
    }

    @Override
    public Result edit(SportType Sport) {
        int update = sportTypeMapper.update(Sport);
        if (update <= 0) {
            return new Result(false, MessageConstant.UPDATE_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.UPDATE_SPORT_SUCCESS);
    }
}
